$(document).ready(function () {

    var socket = io.connect("http://"+$("#server").text()+":8080");
    socket.emit('customOnline', {id: $.cookie("userId")});

    var map_instance = null;
    var input_instance = null;

    var scrollOption = {
        preset: 'datetime',
        minDate: new Date(),
        maxDate: new Date(2020, 12, 30, 59, 59),
        stepMinute: 5,
        theme: 'default',
        mode: 'mixed',
        display: 'bottom',
        lang: 'zh'
    };

    $('#rentBusiness').click(function () {
        var postData = {};
        /**
         *  order_type : RENT_NO_DRIVER,RENT_WITH_DRIVER,TRAVEL_RENT,GO_AIRPORT,OFF_AIRPORT,PIECE
         * */
        postData.start_time = $("#startTime").val();
        postData.end_time = $("#endTime").val();
        postData.start_addr = {};
        postData.end_addr = {};
        postData.start_addr.name = $("#startAddr").val();
        postData.start_addr.geo_point = $("#startAddr").attr('geo');
        postData.end_addr.name = $("#endAddr").val();
        postData.end_addr.geo_point = $("#endAddr").attr('geo');
        postData.distance = window.totalDistance;
        postData.price = $("#total_fee").text();

        if ($("#carModel").length != 0) {
            postData.car_info = {};
            postData.car_info.require_car_type = $("#carModel").val();
        } else {
            if ($(".air-shadow").attr("id") == 'goAirport') {
                postData.order_type = "GO_AIRPORT";
            } else {
                postData.order_type = "GO_AIRPORT";
            }
            postData.customer_name = $("#customerName").val();
            postData.customer_tel = $("#customerTel").val();
        }

        if ($("#totalCount").length != 0) {
            postData.order_type = "TRAVEL_RENT";
            postData.total_count = $("#totalCount").val();
        }

        if ($("#hasDriver:checked").val() == "0") {
            postData.order_type = "RENT_NO_DRIVER";
        } else if ($("#hasDriver:checked").val() == "1") {
            postData.order_type = "RENT_WITH_DRIVER";
        }

        switch ($(this).attr("data")) {
            case "rent" :
                if (!postData.end_time) {
                    window.utils.toast('warning', '未填写必填项');
                    return;
                }
                break;
            case "travel" :
                if (!postData.total_count) {
                    window.utils.toast('warning', '未填写必填项');
                    return;
                }
                break;
            case "airport" :
                if (!postData.customer_tel || !postData.customer_name) {
                    window.utils.toast('warning', '未填写必填项');
                    return;
                }
                break;
        }

        if (postData.start_time && postData.start_addr && postData.end_addr) {
            postData.price =
                $.post('/rent/genOrder', {order: JSON.stringify(postData)}, function (data) {
                    if (data.redirect) {
                        window.utils.toast('warning', "您还未登录或登录过期");
                        setTimeout(function () {
                            window.location = data.redirect;
                        }, 2000);
                    } else {
                        window.utils.toast('success', "订单已生成");
                        socket.on("orderEnsure", function (driver) {
                            window.utils.toast('success', "订单已被司机接收");
                            console.log(driver);
                        });
                    }
                });
        } else {
            window.utils.toast('warning', "订单信息填写不完整");
        }
    });

    $(".geo-location").each(function () {
        $(this).click(function () {
            map_instance = window.utils.bdMap();
            input_instance = this;
            $("#geoInput").focus();
        });
    });

    $(".geo-sure").each(function () {
        $(this).click(function () {
            map_instance && map_instance.closeMap();
            $(input_instance).val(window.unique_marker.m_addr_info);
            $(input_instance).attr("geo", window.unique_marker.point.lng + "," + window.unique_marker.point.lat);
            $(input_instance).change();
        });
    });

    $(".geo-cancel").each(function () {
        $(this).click(function () {
            map_instance && map_instance.closeMap();
        });
    });

    $(".form-datetime").each(function () {
        $(this).scroller(scrollOption);
    });

    window.sureFee = function(type){
        if(type === 'travel') {
            //todo 旅游包车计费方式
            $("#total_fee").text(300);
            return;
        }
        if(!!$("#startTime").val() && !!$("#startAddr").val() && !!$("#endAddr").val()) {
            var tempStart = $("#startAddr").attr('geo');
            var tempEnd = $("#endAddr").attr('geo');
            window.utils.mapSearch(tempStart, tempEnd, map_instance,function(result){
                window.totalDistance = result.distance;
                var price = getTotalCosts(result.distance,$("#startTime").val());
                $("#total_fee").text(price.toFixed(2));
            });
        }
    };

    /*
     车费=起步价8元+每公里1.6元+超出9公里外每公里1.8元
     夜间其余不变，11点至早晨7点每公里2.4元
     */
    var getTotalCosts = function (distance,time) {
        if(new Date(time).getHours() >= 23 ||new Date(time).getHours() < 7) {
            return distance < 9 ? (distance * 1.6 + 8) : ( (distance - 9) * 1.8 + 1.6 * 9 + 8);
        } else {
            return (distance * 2.4 + 8);
        }
    }

});